package com.mashibing.service.impl;

import com.mashibing.internalcommon.constant.AmapConfigConstants;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.response.DirectionResponse;
import com.mashibing.service.DirectionService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class DirectionServiceImpl implements DirectionService {

    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;
    @Override
    public ResponseResult driving(String depLongitude, String depLatitude, String destLongitude, String destLatitude) {
        // 组装url
        StringBuilder urlBuild = new StringBuilder();
        urlBuild.append(AmapConfigConstants.DIRECTION_URL);
        urlBuild.append("?");
        urlBuild.append("origin="+depLongitude+","+depLatitude);
        urlBuild.append("&");
        urlBuild.append("destination="+destLongitude+","+destLatitude);
        urlBuild.append("&");
        urlBuild.append("extensions=base");
        urlBuild.append("&");
        urlBuild.append("output=json");
        urlBuild.append("&");
        urlBuild.append("key="+amapKey);
        log.info("高德地图：路径规划，请求信息："+urlBuild);
        // 请求远程地址
        ResponseEntity<String> directionEntity = restTemplate.getForEntity(urlBuild.toString(), String.class);
        String directionString = directionEntity.getBody();
        log.info("高德地图：路径规划，返回信息："+directionString);

        // 数据解析
        DirectionResponse directionResponse = parseDirectionEntity(directionString);
        return ResponseResult.success(directionResponse);
    }

    private DirectionResponse parseDirectionEntity(String directionString) {
        DirectionResponse directionResponse = null;
        try {
            JSONObject result = JSONObject.fromObject(directionString);
            if (result.has(AmapConfigConstants.STATUS)) {
                String status = result.get(AmapConfigConstants.STATUS).toString();
                if (status.equals("1")) {
                    if (result.has(AmapConfigConstants.ROUTE)) {
                        JSONObject routeObj = result.getJSONObject(AmapConfigConstants.ROUTE);
                        if (routeObj.has(AmapConfigConstants.PATHS)) {
                            JSONArray pathArray = routeObj.getJSONArray(AmapConfigConstants.PATHS);
                            JSONObject obj = pathArray.getJSONObject(0);

                            directionResponse = new DirectionResponse();
                            if (obj.has(AmapConfigConstants.DISTANCE)) {
                                directionResponse.setDistance(obj.getInt(AmapConfigConstants.DISTANCE));
                            }

                            if (obj.has(AmapConfigConstants.DURATION)) {
                                directionResponse.setDuration(obj.getInt(AmapConfigConstants.DURATION));
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return directionResponse;
    }

























}
